#### Replication Code for:
#### Constituency Service and Electoral Accountability in Autocratic Legislatures
#### Author: Erin York
#### Created: 5/30/2024

# this code replicates Figure A4 and Table A15 in the supplementary appendix
# this code requires access to geocoded Morocco Round 6 Afrobarometer data
# see READ ME file for instructions on how to access this dataset

rm(list = ls())

## Load Packages
library(tidyverse)
library(stargazer)
library(sandwich)

library(logr)

options("logr.autolog" = TRUE)

# Open the log
lf <- log_open(file.path("logexternal.log"))

# Send message to log
log_code()



# read in data ------------------------------------------------------------

# afrobarometer data - adjust filepath as necessary
afro_r6<- read_csv("MOR R6 Geocoded_fixed.csv")


# geolink for afrobarometer + census indicators
mergedat<- read_csv("df_afrolink.csv")




# recode afrobarometer variables ------------------------------------------

afro_subset<- afro_r6 %>%
  group_by(respno) %>%
  # demographic controls
  rename(age = q1) %>% # respondent age
  mutate(poverty_hunger = as.numeric(q8b > 0), # gone without enough food to eat in past year
         socialmedia = as.numeric(q12e > 0), # coded 1 for sometimes get news from social media
         voted_2011 = as.numeric(q21 == 1), # coded 1 for voted in november 2011
         female = as.numeric(q101 == 2), # coded 1 for women
         edu_full = ifelse(q97 > 10, NA, q97), # education on 9 pt scale none to postgrad
         reli_full = ifelse(q98b > 7|q98b < 0, NA, q98b), # religious practice from never to more than once/day
         unemployed = as.numeric(q95 == 1), # respondent has no job and is looking for work
         rifi = as.numeric(q87 == 1501), # identifies as rifi
         soussi = as.numeric(q87 == 1502), # identifies as soussi
         chalh = as.numeric(q87 == 1503), # identifies as chalh
         sahraoui = as.numeric(q87 == 1504)) %>% # identifies as sahraoui
  ## imputed controls (median value)
  mutate(reli_full_imp = ifelse(is.na(reli_full), 3, reli_full),
         edu_full_imp = ifelse(is.na(edu_full), 4, edu_full),
         no_internet = as.numeric(q92b == 0)) %>% # never use internet
  # political attitudes
  ## political leanings
  mutate(vote_opp = ifelse(q99 %in% c(1500, 1501, 1504, 1507), 1, 0),
         vote_roy = ifelse(q99 %in% c(1502, 1503, 1505, 1506), 1, 0),
         close_party = as.numeric(q90a == 1)) %>% # respondent feels close to a political party
  ## government approval/responsiveness/corruption
  mutate(mun_approve = ifelse(q68c == 9, NA, q68c), # approves of performance (4pt scale) in last 12 months
         mp_approve = ifelse(q68b == 9, NA, q68b),
         mp_listen = ifelse(q59a == 9, NA, q59a), # how often does X listen to ppl like you?
         mun_listen = ifelse(q59b == 9, NA, q59b),
         mp_corrupt = ifelse(q53b > 4, NA, q53b), 
         mun_corrupt = ifelse(q53d > 4, NA, q53d)
         ) %>%
  ungroup() %>%
  dplyr::select(respno, region, locationlevel1, locationlevel2, townvill,
         voted_2011, no_internet, age, poverty_hunger, urbrur, socialmedia,
         unemployed, rifi, sahraoui, chalh, soussi,
         reli_full_imp, edu_full_imp, female,
         vote_roy, vote_opp,
         mp_corrupt, mun_corrupt,
         mp_approve, mun_approve,
         mp_listen, mun_listen) %>%
  group_by(respno) %>%
  mutate(votechoice = ifelse(vote_roy + vote_opp == 0, "No Party", ifelse(vote_roy == 1, "Royalist",
                                                                          "Opposition")),
         amazigh = as.numeric(rifi + sahraoui + chalh + soussi > 0),
         reli_never = as.numeric(reli_full_imp == 0), # never practice religion
         reli_often = as.numeric(reli_full_imp > 3), # practice religion more than 1x per week
         edu_low = as.numeric(edu_full_imp < 4), # primary school or less
         edu_high = as.numeric(edu_full_imp > 5)) %>%
  mutate(gov_corrupt = mean(c(mp_corrupt, mun_corrupt), na.rm = T),
         gov_approve = mean(c(mp_approve, mun_approve), na.rm = T),
         gov_listen = mean(c(mp_listen, mun_listen), na.rm = T)) %>%
  ungroup() %>%
  mutate(age_std = scale(age),
         corrupt_std = scale(gov_corrupt),
         approve_std = scale(gov_approve),
         listen_std = scale(gov_listen))


# merge with census and activity data -------------------------------------

afro_full<- afro_subset %>% left_join(mergedat) 

# figure a4 ---------------------------------------------------------------

roy_pref<- lm(vote_roy ~ age_std + poverty_hunger + urbrur + socialmedia + unemployed +
                amazigh + reli_never + reli_often + edu_low + edu_high, 
              filter(afro_full, votechoice != "No Party"))

fa4<- data_frame(x = coef(roy_pref), xlow = confint(roy_pref)[,1], xhi = confint(roy_pref)[,2],
                 var = names(coef(roy_pref))) %>%
  filter(var != "(Intercept)") %>%
  mutate(Attribute = c("Age", "Poor", "Rural", "Uses Social Media",
                       "Unemployed", "Amazigh", "Religion - never", "Religion - often",
                       "Education - low", "Education - high")) %>%
  mutate(attribute = reorder(Attribute, -x)) %>%
  ggplot(aes(x = attribute)) +
  geom_hline(aes(yintercept = 0), color = "Gray") +
  geom_pointrange(aes(y = x, ymin = xlow, ymax = xhi)) +
  coord_flip() +
  theme_bw() + 
  ylab("Prefer Royalist Party") + 
  xlab("") 
fa4

ggsave(fa4, filename = "FigureA4.pdf",
       width = 8, height = 5)



# table a15 ---------------------------------------------------------------

# gov approval
app<- lm(approve_std ~ log(anyref + 1), afro_full)
appc2<- lm(approve_std ~ log(anyref + 1) +
             log(population)+ illiteracy + unemployment + rural_housing  +Darija +
             voted_2011+ no_internet+ age_std + poverty_hunger + urbrur + socialmedia + 
             unemployed + amazigh + reli_never + reli_often + 
             edu_low + edu_high  +female, afro_full)

list<- lm(listen_std ~ log(anyref + 1), afro_full)
listc2<- lm(listen_std ~ log(anyref + 1) +
              log(population)+ illiteracy + unemployment + rural_housing  +Darija +
              voted_2011+ no_internet+ age_std + poverty_hunger + urbrur + socialmedia + 
              unemployed + amazigh + reli_never + reli_often + 
              edu_low + edu_high +female, afro_full)

corr<- lm(corrupt_std ~ log(anyref + 1), afro_full)
corrc2<- lm(corrupt_std ~ log(anyref + 1) +
              log(population)+ illiteracy + unemployment + rural_housing  +Darija +
              voted_2011+ no_internet+ age_std + poverty_hunger + urbrur + socialmedia + 
              unemployed + amazigh + reli_never + reli_often + 
              edu_low + edu_high  +female, afro_full)

sel<- sqrt(diag(vcovCL(list, cluster = afro_full$commcode)))
selc2<- sqrt(diag(vcovCL(listc2, cluster = afro_full$commcode)))

sea<- sqrt(diag(vcovCL(app, cluster = afro_full$commcode)))
seac2<- sqrt(diag(vcovCL(appc2, cluster = afro_full$commcode)))

sec<- sqrt(diag(vcovCL(corr, cluster = afro_full$commcode)))
secc2<- sqrt(diag(vcovCL(corrc2, cluster = afro_full$commcode)))

sink("TableA15.tex")
stargazer( app, appc2,list, listc2, corr, corrc2,
           se = list(sea, seac2, sel, selc2, sec, secc2),
           covariate.labels = c("Log Municipal References",
                                "Log population", "Illiteracy Rate", "Unemployment Rate", "Prop. Rural Housing","Prop. Darija",
                                "Voted in 2011", "No Internet", "Age (standardized)", "Impoverished", "Rural", "Social Media User",
                                "Unemployed", "Amazigh", "Religion - never", "Religion - often",
                                "Education - low", "Education - high", "Female"),
           dep.var.labels = c("Gov. Approval", "Gov. Listens", "Gov. Corrupt"),
           omit.stat = c("ser", "f"),
           no.space = T)
sink()

####
log_close()
